-- RewardAmazingBoxRender

local ClassRef = app.mvc.createRenderClass(...)

local NEED_BUY_ITEM_STR = L("mountticket_notenough")

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AmazingBoxRender))
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._ui["Panel/Button_buy/Button"]:setScale(0.8, 0.9)
    self._ui["Panel/Button_buy/Button"]:addClickEventListener(function()
        self._ui["Panel/Button_buy/Button"]:setScale(0.8, 0.9)
        local needNum = self._costData:getAmount()
        local info = app:getInst("RewardModel"):parseMoneyInfo()
        if info and info.num < needNum then
            local str = string.format(NEED_BUY_ITEM_STR, self._costData:getName())
            app:sendMsg("MallController", "openNeedBuyDialog", str, self._costData:getId())
        else
            self._ui["Panel/Button_buy/Button"]:setTouchEnabled(false)
            executeFunc(self._onOperate, "openAmazingAward", self._packageId)
        end
    end)
end

function ClassRef:setData(data)
    self._data = data
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        self._packageId = data.packageID
        self._dropID = data.dropID
        local conf = GD:getActivityAmazingAwardById(data.packageID)
        if not conf then return end

        self._ui.tlt:setString(L(conf.name))
        local tltIconPath = string.format("%s%s.png",Res.activityIconPath, conf.titleIcon)
        self._ui.bg_tlt:loadTexture(tltIconPath)
        local backPath = string.format("%s%s.png",Res.activityIconPath, conf.backIcon)
        self._ui.bg:loadTexture(backPath)

        local percent = data.dropChance / 100
        self._ui["Panel/text"]:setString(string.format(L("activity_amazingaward_string3"), percent))
        local loadingBarPer = math.floor(data.nowTimes/(data.maxTimes-1)*100)
        self._ui.LoadingBar:setPercent(loadingBarPer)
        if data.isGet then
            self._ui["Panel/Panel_bar/text"]:setString(L("activity_amazingaward_string4"))
            self._ui.LoadingBar:setPercent(100)
            self._ui["Panel/text"]:setString(string.format(L("activity_amazingaward_string3"), 100))
        elseif data.nowTimes+1 == data.maxTimes then
            self._ui["Panel/Panel_bar/text"]:setString(L("activity_amazingaward_string2"))
            self._ui["Panel/text"]:setString(string.format(L("activity_amazingaward_string3"), 100))
        else
            self._ui["Panel/Panel_bar/text"]:setString(string.format(L("activity_amazingaward_string1"), (data.maxTimes-data.nowTimes)))            
        end
        self._costData = UD:parseLootToBagItem(data.cost)
        local btnNode = self._ui["Panel/Button_buy"]
        local iconPath = string.format("%s%s.png",Res.itemPath, conf.coinIcon)
        btnNode:getChildByName("icon"):loadTexture(iconPath)
        btnNode:getChildByName("num"):setString(self._costData:getAmount())
        display.setBtnTouchEnable(btnNode:getChildByName("Button"), not data.isGet)
        -- 保底次数为1表示为日怒黑市活动（按钮显示为购买,进度条概率文字提示都不显示,背景图宝箱也不同）
        local boxIconPath = string.format("%s%s.png",Res.activityIconPath, conf.boxIcon)
        self._ui.icon_1:setVisible(false)
        self._ui.icon_rinu:setVisible(false)        
        if data.maxTimes == 1 then
            self._ui.icon_rinu:setVisible(true)
            self._ui["Panel/icon_rinu/icon1"]:loadTexture(boxIconPath)
            self._ui["Panel/Panel_bar"]:setVisible(false)
            self._ui["Panel/text"]:setVisible(false)
            btnNode:getChildByName("Text_name"):setString(L("ui_csd_995"))
        else
            self._ui.icon_1:setVisible(true)
            self._ui["Panel/icon_1/icon"]:loadTexture(boxIconPath)
            self._ui["Panel/Panel_bar"]:setVisible(true)
            self._ui["Panel/text"]:setVisible(true)
            btnNode:getChildByName("Text_name"):setString(L("ui_csd_253"))
        end
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef